package view;

import Bean.Customer;
import control.CustomerManagerControl;
import java.awt.HeadlessException;
import javax.swing.JOptionPane;

public class AddCustomerDialog extends RootDialog
{

    private CustomerCategoryPanel Panel;
    private CustomerManagerControl cmControl = new CustomerManagerControl();

    public AddCustomerDialog(CustomerCategoryPanel Panel)
    {
        initComponents();
        this.Panel = Panel;
        this.setTitle("Thêm khách hàng");
        super.Init();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jLabel1 = new javax.swing.JLabel();
        jTFHoten = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTFNămsinh = new javax.swing.JTextField();
        jTFCMTND = new javax.swing.JTextField();
        jTFDienthoai = new javax.swing.JTextField();
        jTFDiachi = new javax.swing.JTextField();
        jBThemkhachhang = new javax.swing.JButton();
        jCBGioiTinh = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 153));
        jLabel1.setText("Họ tên");

        jTFHoten.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 51, 153));
        jLabel2.setText("Năm sinh");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 51, 153));
        jLabel3.setText("Giới tính");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(0, 51, 153));
        jLabel4.setText("CMTND");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 51, 153));
        jLabel5.setText("Điện thoại");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 51, 153));
        jLabel6.setText("Địa chỉ");

        jTFNămsinh.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTFCMTND.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTFDienthoai.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTFDiachi.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jBThemkhachhang.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jBThemkhachhang.setText("Thêm khách hàng");
        jBThemkhachhang.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBThemkhachhangActionPerformed(evt);
            }
        });

        jCBGioiTinh.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jCBGioiTinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nam", "Nữ" }));

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/add_customer.png"))); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLabel8)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel1)
                                    .add(jLabel2)
                                    .add(jLabel3))
                                .add(24, 24, 24)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(jTFNămsinh)
                                    .add(jTFHoten)
                                    .add(jCBGioiTinh, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 171, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(layout.createSequentialGroup()
                                .add(jLabel4)
                                .add(33, 33, 33)
                                .add(jTFCMTND))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel5)
                                    .add(jLabel6))
                                .add(18, 18, 18)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jTFDienthoai)
                                    .add(jTFDiachi))))
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jBThemkhachhang)
                        .add(72, 72, 72))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(16, 16, 16)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTFHoten, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jTFNămsinh, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jCBGioiTinh, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jTFCMTND, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jTFDienthoai, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel6)
                    .add(jTFDiachi, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(27, 27, 27)
                .add(jBThemkhachhang, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(0, 14, Short.MAX_VALUE)
                .add(jLabel8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBThemkhachhangActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jBThemkhachhangActionPerformed
    {//GEN-HEADEREND:event_jBThemkhachhangActionPerformed
        Customer Customer = new Customer();
        Customer.setName(jTFHoten.getText());
        try
        {
            Customer.setBrithday(Integer.parseInt(jTFNămsinh.getText()));
            Customer.setPhone(Long.parseLong(jTFDienthoai.getText()));
            Customer.setGender(jCBGioiTinh.getSelectedItem().toString());
            Customer.setCMTND(jTFCMTND.getText());
            Customer.setAdress(jTFDiachi.getText());
            if (isNotNullNotEmptyNotWhiteSpace(jTFDiachi.getText()) == false || isNotNullNotEmptyNotWhiteSpace(jTFCMTND.getText()) == false || isNotNullNotEmptyNotWhiteSpace(jTFHoten.getText()) == false || (Customer.getBrithday() > 2012) || (Customer.getBrithday() <= 0) || Customer.getName().equals("") || Customer.getBrithday() == 0 || Customer.getGender().equals("") || Customer.getCMTND().equals("") || Customer.getPhone() == 0 || Customer.getAdress().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
            }
            else
            {
                cmControl.addCustomer(Customer);
                JOptionPane.showMessageDialog(null, "Nhập thành công");
                Panel.reset();
                reset();
            }
        }
        catch (NumberFormatException | HeadlessException ex)
        {
            JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
        }
    }//GEN-LAST:event_jBThemkhachhangActionPerformed
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(AddCustomerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(AddCustomerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(AddCustomerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(AddCustomerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBThemkhachhang;
    private javax.swing.JComboBox jCBGioiTinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTFCMTND;
    private javax.swing.JTextField jTFDiachi;
    private javax.swing.JTextField jTFDienthoai;
    private javax.swing.JTextField jTFHoten;
    private javax.swing.JTextField jTFNămsinh;
    // End of variables declaration//GEN-END:variables

    private void reset()
    {
        jTFHoten.setText(null);
        jTFNămsinh.setText(null);
        jCBGioiTinh.setSelectedItem(null);
        jTFCMTND.setText(null);
        jTFDienthoai.setText(null);
        jTFDiachi.setText(null);
    }

    public static boolean isNotNullNotEmptyNotWhiteSpace(final String string)
    {
        return string != null && !string.isEmpty() && !string.trim().isEmpty();
    }
}
